<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/22
 * Time: 14:15
 */

namespace App\Modules\SendOrder\Models;

use DB;
use Illuminate\Database\Eloquent\Model;
use Redis;

class SendOrder extends Model
{
    protected $table = 'send_orders';
    protected $fillable = [
        'name',
        'channel_type',
        'promotion_type',
        'distribution_channel_id',
        'book_id',
        'book_name',
        'chapter_id',
        'chapter_name',
        'headline_id',
        'body_template_id',
        'document_cover_id',
        'original_guide_id',
        'subscribe_chapter_id',
        'import_company_name',
        'fan_num',
        'subscribe_chapter_name',
        'qr_code_id',
        'subscribe_chapter_seq',
        'cost',
        'sex_preference',
        'is_enable',
        'entrance',
        'domain',
        'remark',
        'redirect_url',
        'send_time',
        'charge_type',
        'pre_send_date',
        'promotion_point',
        'force_show_qrcode'
    ];

    static function searchForDaliy($params, $isAll = false)
    {
        $search_object = self::where('is_enable', 1);
        $search_object->select('id','book_id', 'name', 'promotion_type', 'created_at', 'remark', 'import_company_name', 'send_time', 'promotion_point', 'pre_send_date', 'book_name', 'distribution_channel_id', 'cost');

        if (isset($params['distribution_channel_ids']) && $params['distribution_channel_ids']) {
            $search_object->whereIn('distribution_channel_id', $params['distribution_channel_ids']);
        }

        if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
            $search_object->where('distribution_channel_id', $params['distribution_channel_id']);
        }

        if (isset($params['start_time']) && $params['start_time']) {
            $search_object->where('send_time', '>=', $params['start_time']);
        }

        if (isset($params['end_time']) && $params['end_time']) {
            $search_object->where('send_time', '<=', $params['end_time']);
        }

        if (isset($params['promotion_type']) && $params['promotion_type']) {
            $search_object->where('promotion_type', $params['promotion_type']);
        }

        $search_object->orderBy('distribution_channel_id');

        //\Log::info($search_object->toSql());
        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

    /**
     * 获取派单总pv
     * @param $id
     * @return int
     */
    public static function getSendOrderTotalPv($id)
    {
        $today = (int)Redis::hget('send_order_pv_' . $id, date('Y-m-d'));
        $old = (int)DB::table('wap_visit_stats')
            ->where('key', $id)->where('type', 3)
            ->where('from_type', 'send_orders')
            ->sum('pv');
        return $today + $old;
    }

    /**
     * 获取派单总pv
     * @param $id int
     */
    public static function getSendOrderTotalUv($id)
    {
        //return (int)Redis::hget('send_order_uv_' . $id, 'total');
        $today = (int)Redis::hget('send_order_uv_'.$id,date('Y-m-d'));
        $old = (int)DB::table('wap_visit_stats')->where('key', $id)->where('type', 3)->where('from_type','send_orders')->sum('uv');
        return $today+$old;
    }

    /**
     *  更新派单的星数、备注
     * @param $id  派单id
     * @param $distribution_channel_id 渠道id
     * @param $remark 备注
     * @return mixed
     */
    static function updateRemark($id, $distribution_channel_id, $remark)
    {
        return self::where('id', $id)
            ->where('distribution_channel_id', $distribution_channel_id)
            ->update(['remark' => $remark]);
    }

    public static function getDuringCost($distribution_channel, $begin_time, $end_time)
    {
        return self::where('distribution_channel_id', $distribution_channel)
            ->where('is_enable', 1)
            ->where('pre_send_date', '>=', $begin_time)
            ->where('pre_send_date', '<=', $end_time)
            ->sum('cost');
    }

    public static function getDuringCostByChannelIds($distribution_channels, $begin_time, $end_time)
    {
        $search_obj = self::where('is_enable', 1)
            ->where('pre_send_date', '>=', $begin_time)
            ->where('pre_send_date', '<=', $end_time)
            ->whereIn('distribution_channel_id', $distribution_channels);
        return $search_obj->sum('cost');
    }

    public static function getDuringCostByChannelId($distribution_channel, $end_time)
    {
        $search_obj = self::where('is_enable', 1)
            ->where('send_time', '<=', $end_time)
            ->where('distribution_channel_id', $distribution_channel);
        return $search_obj->sum('cost');
    }

    public static function getDuringCostByDistributionChannelIds($distribution_channels, $end_time)
    {
        $search_obj = self::where('is_enable', 1)
            ->where('send_time', '<=', $end_time)
            ->whereIn('distribution_channel_id', $distribution_channels);
        return $search_obj->sum('cost');
    }

    public static function getTotalCost($distribution_channel_id_array)
    {
        $search_obj = self::orderBy('id', 'desc');
        $search_obj->whereIn('distribution_channel_id', $distribution_channel_id_array)->whereNotNull('send_time');
        return $search_obj->sum('cost');
    }
}